package com.bk.bookviews.controller;

import com.bk.bookviews.common.Constants;
import com.bk.bookviews.common.Pagination;
import com.bk.bookviews.domain.ResultMsg;
import com.bk.bookviews.domain.UserInfo;
import com.bk.bookviews.exception.BusinessException;
import com.bk.bookviews.exception.BusinessExceptionEnum;
import com.bk.bookviews.service.UserInfoService;
import com.bk.bookviews.util.PageUtils;
import com.bk.bookviews.util.RedisUtil;
import com.bk.bookviews.util.StringUtil;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/bk/userInfo")
public class UserInfoController {

    @Autowired
    RedisUtil redisUtil;

    @Autowired
    UserInfoService userInfoService;

    @PostMapping("/register")
    public ResultMsg register( @RequestBody UserInfo userInfo) throws BusinessException {
        //手机号不能为空
        if (StringUtil.isBlank(userInfo.getUserPhone())) {
            throw new BusinessException(BusinessExceptionEnum.PHONE_IS_NULL);
        }
        //密码不能为空
        if (StringUtil.isBlank(userInfo.getUserPwd())) {
            throw new BusinessException(BusinessExceptionEnum.PWD_IS_NULL);
        }
        userInfo.setUserRole(Constants.SIMPLE_USER);
        userInfo.setUserStatus(Constants.NORAMAL);
        System.out.println(userInfo);
        userInfoService.register(userInfo);
        return ResultMsg.successResult();
    }

    @PostMapping("/login")
    public ResultMsg login(@RequestBody UserInfo userInfo) throws BusinessException {
        //手机号不能为空
        if (StringUtil.isBlank(userInfo.getUserPhone())) {
            throw new BusinessException(BusinessExceptionEnum.PHONE_IS_NULL);
        }
        //密码不能为空
        if (StringUtil.isBlank(userInfo.getUserPwd())) {
            throw new BusinessException(BusinessExceptionEnum.PWD_IS_NULL);
        }
        return ResultMsg.successResult(userInfoService.login(userInfo));
    }

    @GetMapping()
    public ResultMsg selectUserInfo(Long userId) {
        UserInfo userInfo = userInfoService.selectUser(userId);
        return ResultMsg.successResult(userInfo);
    }

    @PostMapping("/userList")
    @ResponseBody
    public Pagination selectUserPage(@RequestBody UserInfo userInfo, @RequestHeader("pageNum") String pageNum, @RequestHeader("pageSize") String pageSize) {
        PageHelper.startPage(Integer.parseInt(pageNum),Integer.parseInt(pageSize));
        return PageUtils.getPagination(userInfoService.selectUserList(userInfo));
    }

    @GetMapping("/token")
    public ResultMsg selectUserInfoByToken(@RequestHeader("token") String token) {
        UserInfo userInfo = (UserInfo) redisUtil.get(token);
        if (userInfo == null){
            throw new BusinessException(BusinessExceptionEnum.LOGON_FAILURE);
        }
        return ResultMsg.successResult(userInfo);
    }

    @PutMapping()
    public ResultMsg editSave(@RequestBody UserInfo userInfo) throws BusinessException {
        if (userInfo.getUserId() == null){
            throw new BusinessException(BusinessExceptionEnum.USER_ID_IS_NULL);
        }
        userInfoService.updateUser(userInfo);
        return ResultMsg.successResult();
    }
}
